/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.web.rest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Builds a plain text jax rs response (status code=200, content type=text/plain)
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 22:38 20.08.11
 *
 * @author Herbert
 * @version 1.0
 */
public class PlainTextResponseBuilder {

    /** The logger. */
    private static final Logger logger = LoggerFactory.getLogger(PlainTextResponseBuilder.class);

    private final UriInfo uriInfo;

    private final StringWriter result = new StringWriter();
    private final PrintWriter out = new PrintWriter(result);

    private Map<String, String> customHeaderParameters = new HashMap<>();

    public PlainTextResponseBuilder(UriInfo uriInfo) {
        this.uriInfo = uriInfo;
    }


    public void println(String s) {
        out.println(StringUtils.trim(StringUtils.join(new Object[]{uriInfo != null ? uriInfo.getRequestUri() : null, ": ", s})));
        logger.debug(s);
    }

    public Response getResponse() {
        Response.ResponseBuilder responseBuilder = Response.ok().type(MediaType.TEXT_PLAIN_TYPE).entity(result);

        // add custom header parameters if there are any
        if (!customHeaderParameters.isEmpty()) {
            for (Map.Entry<String, String> customHeaderParameter : customHeaderParameters.entrySet()) {
                responseBuilder.header(customHeaderParameter.getKey(), customHeaderParameter.getValue());
            }
        }

        return responseBuilder.build();
    }

    public Response getServerErrorResponse() {
        return Response.serverError().type(MediaType.TEXT_PLAIN_TYPE).entity(result).build();
    }
    
    public void addCustomHeaderParameter(String headerParameterName, String value) {
        customHeaderParameters.put(headerParameterName, value);
    }

}
